import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

//静态路由
export const constantRoutes = [
  {//登录
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {//404
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {//首页
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: 'home',
      name: 'Home',
      component: () => import('@/views/home/index'),
      meta: { title: '首页', icon: 'home' }
    }]
  },
  //任意页
  { path: '*', redirect: '/404', hidden: true }
]

//动态路由
export const allAsyncRoutes = [
  {//商品管理
    path: '/product',
    component: Layout,
    redirect: '/product/attr',
    name: 'Product',
    meta: { title: '商品管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'attr',
        name: 'Attr',
        component: () => import('@/views/product/attr/index'),
        meta: { title: '平台属性管理', icon: 'tree' }
      }
    ]
  },
  {//权限管理
    path: '/acl',
    component: Layout,
    redirect: '/acl/user',
    name: 'Acl',
    meta: { title: '权限管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/acl/user/index'),
        meta: { title: '用户管理', icon: 'user' }
      },
      {
        path: 'person',
        name: 'Role',
        component: () => import('@/views/acl/person/index'),
        meta: { title: '角色管理', icon: 'table' }
      },
      {
        path: 'tree',
        name: 'RoleAuth',
        component: () => import('@/views/acl/person/tree'),
        meta: { title: '角色管理' },
        hidden: true //左侧菜单栏隐藏该菜单
      }
    ]
  }
]


const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
